package interceptor;

import action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 实现简单的拦截器
 * 配置在LoginAction上
 */
public class SimpleInterceptor extends AbstractInterceptor{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        //获得被拦截的Action的示例
//        LoginAction action = (LoginAction) actionInvocation.getAction();
        //在拦截器中注册PreResultListener监听器
        actionInvocation.addPreResultListener(new MyPreResultListener());
        System.out.println("-----"+name+"开始执行拦截的Action-----");
        long start = System.currentTimeMillis();
        String result = actionInvocation.invoke();
        long end = System.currentTimeMillis();
        System.out.println("-----"+name+"被拦截的Action执行完成-----");
        System.out.println("-----"+name+"拦截耗时"+(end-start)+" ms-----");
        System.out.println("-----invoke方法返回的结果为: "+result+"-----");
        return result;
    }
}
